open Arbreintermediaire
open Ast
open Frame

let rec afficherOperateur operateur =
	match operateur with
	| Plus  -> print_char('+')
	| Moins -> print_char('-')
	| Mult	-> print_char('*')
	| Div	-> print_char('/')

let rec afficherComparateur comparateur =
	match comparateur with
	| Inf 	  -> print_char('<')
	| Sup     -> print_char('>')
	| InfEgal -> print_endline("<=")
	| SupEgal -> print_endline(">=")
	| Egal    -> print_endline("==")
	| Diff	  -> print_endline("<>")

let rec afficherFrame frame =
	print_endline(frame.name)

let rec afficherRegister register =
	match register with
	| UnReg(a,b) -> begin
	 				print_endline(a);
	 				print_int(b);
	 				end


let rec afficherLabel iLabel =
	match iLabel with
	| Nom(a,b) -> begin
					print_endline(a);
					print_int(b);
				  end

let rec afficherExpression iExpression =
	match iExpression with
	| Const(a)    -> print_int(a)
	| Reg(a)    -> afficherRegister(a)
	| Bin(a,b,c)  -> begin
						afficherOperateur(a);
						afficherExpression(b);
						afficherExpression(c);
					 end
	| Comp(a,b,c) -> begin
						afficherComparateur(a);
						afficherExpression(b);
						afficherExpression(c);			
					 end

let rec afficherFunCall iFunCall =
	match iFunCall with
	| Fun(a,b) -> 	begin
						afficherFrame(a);
						List.iter (fun t -> afficherExpression(t)) b
						;
					end

let rec afficherGoto iGoto =
	match iGoto with
	| Label(a) ->	begin
						print_endline("goto");
						afficherLabel(a);
					end

let rec afficherEgal iEgal =
	match iEgal with
	| RegC(a,b) -> 	begin
						afficherRegister(a);
						print_int(b);
					end
	| RegF(a,b) ->	begin
						afficherRegister(a);
						afficherFunCall(b);
					end
	| RegE(a,b) ->	begin
						afficherRegister(a);
						afficherExpression(b);
					end


let rec afficherJump iJump =
	match iJump with
	| Cond(a,b,c) ->	begin
							afficherExpression(a);
							afficherLabel(b);
							afficherLabel(c);
						end

let rec afficherStatement iStatement =
	match iStatement with
	| Jump(a) -> afficherJump(a)
	| Arbreintermediaire.Egal(a) -> afficherEgal(a)
	| Goto(a) -> afficherGoto(a)
	| Nop(a)  -> print_endline("Nop")
	| Func(a) -> afficherFunCall(a)

let rec afficherElement iElement =
	match iElement with
	| State(a) -> afficherStatement(a)
	| Exp(a) -> afficherExpression(a)

let rec afficherBlock iBlock =
	match iBlock with
	| Block(a,b) ->	begin
						afficherLabel(a);
						List.iter (fun t -> afficherElement(t)) b
					end